import request from '@/utils/request'

// 查询备件库存列表
export function listSpare(query) {
  return request({
    url: '/sbtz/spare/list',
    method: 'get',
    params: query
  })
}

// 查询备件库存详细
export function getSpare(id) {
  return request({
    url: '/sbtz/spare/' + id,
    method: 'get'
  })
}

// 新增备件库存
export function addSpare(data) {
  return request({
    url: '/sbtz/spare',
    method: 'post',
    data: data
  })
}

// 修改备件库存
export function updateSpare(data) {
  return request({
    url: '/sbtz/spare',
    method: 'put',
    data: data
  })
}

// 删除备件库存
export function delSpare(id) {
  return request({
    url: '/sbtz/spare/' + id,
    method: 'delete'
  })
}

export function listSpares(params) {
  return request({
    url: '/sbtz/spare/lists',
    method: 'get',
    params
  })
}

// POST /system/spare/ids    body: [1,2,3]
export function getSparesByIds(ids) {
  return request({
    url: '/sbtz/spare/ids',
    method: 'post',
    data: ids
  })
}

// 远程搜索备件（按 code/name/spec 模糊）
export function searchSpare(params) {
  // params: { q: '关键字', limit?: 20 }
  return request({
    url: '/sbtz/spare/search',
    method: 'get',
    params
  })
}

// 根据ID取备件详情（用于回填库存等）
export function getSpareById(id) {
  return request({
    url: `/sbtz/spare/${id}`,
    method: 'get'
  })
}


/** 批量新增：一个code一条spare，行内spec/vendor变成variants */
export function addSpareBatch(data) {
  return request({
    url: '/sbtz/spare/batchInsert',
    method: 'post',
    data
  })
}

// 获取厂商列表
export async function getVendorsBySpare(spareId) {
  if (!spareId) return []
  const res = await request({
    url: `/sbtz/spare/vendors/${spareId}`,
    method: 'get',
  })
  return (res.data || []).map(v => ({ label: v, value: v }))
}

// 获取规格列表
export async function getSpecsBySpareVendor(spareId, vendor) {
  if (!spareId || !vendor) return []
  const res = await request({
    url: `/sbtz/spare/specs`,
    method: 'get',
    params: { spareId, vendor },
  })
  return (res.data || []).map(v => ({ label: v, value: v }))
}